using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MM.Business;
using MM.Entities;

namespace MM
{
    public partial class frmHienthiPhieuXK : Form
    {
        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";

        public frmHienthiPhieuXK(string sConn, string MaPhieuXuatHang)
        {
            InitializeComponent();
            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;

            if (MaPhieuXuatHang == "")
            {
                this.HienthiPHIEU_XUAT_HANG_MAX(_matau);
                MaPhieuXuatHang = this.txtSoPhieuGR.Text.Trim();
            }
            else
                this.HienthiPHIEU_XUAT_HANG(MaPhieuXuatHang);
            this.HienthiCHI_TIET_PXH(MaPhieuXuatHang);
            this.HienthiCHI_TIET_GT_ND(MaPhieuXuatHang);
        }

        public frmHienthiPhieuXK(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;

          
                this.HienthiPHIEU_XUAT_HANG_MAX(_matau);
                

                this.HienthiCHI_TIET_PXH(this.txtSoPhieuGR.Text.Trim());
                this.HienthiCHI_TIET_GT_ND(this.txtSoPhieuGR.Text.Trim());
        }

        

        public void TimKiem(CHI_TIET_PXH obj, int daduyet)
        {
           
            frmTimPhieuXK f = new frmTimPhieuXK(_sConn, obj, daduyet);
            f.chonPXK += new frmTimPhieuXK.ChonPhieuXuatKho(GetPhieuXuatKho);
           // f.Show(this);
        }

        private void HienthiPHIEU_XUAT_HANG_MAX(string matau)
        {
            clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);
            List<PHIEU_XUAT_HANG> lst = new List<PHIEU_XUAT_HANG>();
            lst = pxhCtrl.usp_SelectPHIEU_XUAT_HANG_MAX(matau);
            if (lst.Count > 0)
            {
                this.txtSoPhieuGR.Text = lst[0].MaPhieuXuatHang;
                this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiXuat;

                if (lst[0].NgayExport.HasValue)
                {
                    this.chkDaduyet.Checked = true;
                }
                else
                {
                    this.chkDaduyet.Checked = false;
                }
                if (lst[0].TinhTrang == 1)
                {
                    this.chkXoa.Checked = true;
                }
                else
                {
                    this.chkXoa.Checked = false;
                }
            }

        }
        private void HienthiPHIEU_XUAT_HANG(string MaPhieuXuatHang)
        {
            clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);
            List<PHIEU_XUAT_HANG> lst = new List<PHIEU_XUAT_HANG>();
            lst = pxhCtrl.usp_SelectPHIEU_XUAT_HANG(MaPhieuXuatHang);
            if (lst.Count > 0)
            {
                this.txtSoPhieuGR.Text = lst[0].MaPhieuXuatHang;
                this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiXuat;

                if (lst[0].NgayExport.HasValue)
                {
                    this.chkDaduyet.Checked = true;
                }
                else
                {
                    this.chkDaduyet.Checked = false;
                }
                if (lst[0].TinhTrang == 1)
                {
                    this.chkXoa.Checked = true;
                }
                else
                {
                    this.chkXoa.Checked = false;
                }
            }
            else
            {
                this.txtSoPhieuGR.Text = "";
                this.txtNgayThucHien.Text = "";
                this.txtNguoiThucHien.Text = "";
                this.chkXoa.Checked = false;
                this.chkDaduyet.Checked = false;
            }

        }
        private void HienthiCHI_TIET_PXH(string MaPhieuXuatHang)
        {
            clsCHI_TIET_PXH ctCtrl = new clsCHI_TIET_PXH(_sConn);
            List<CHI_TIET_PXH> lst = new List<CHI_TIET_PXH>();
            lst = ctCtrl.usp_SelectCHI_TIET_PXH_ByMaPhieuXuatHang(MaPhieuXuatHang);
            this.gridControl1.DataSource = lst;
        }
        private void HienthiCHI_TIET_GT_ND(string MaPhieuXuatHang)
        {
            try
            {
                if (MaPhieuXuatHang != "")
                {
                    clsCHI_TIET_ND_GT gtCtrl = new clsCHI_TIET_ND_GT(_sConn);
                    List<CHI_TIET_ND_GT> lstGT = new List<CHI_TIET_ND_GT>();
                    lstGT = gtCtrl.usp_SelectCHI_TIET_ND_GT_byMaGiaoDich(MaPhieuXuatHang);

                    this.gridControl2.DataSource = lstGT;
                }
            }
            catch { }

        }

        private void txtSoPhieuGR_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string maphieuxuatkho = this.txtSoPhieuGR.Text.Trim();
                this.HienthiPHIEU_XUAT_HANG(maphieuxuatkho);
                this.HienthiCHI_TIET_PXH(maphieuxuatkho);
                this.HienthiCHI_TIET_GT_ND(maphieuxuatkho);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
            frmTimPhieuXK f = new frmTimPhieuXK(_sConn);
            f.chonPXK += new frmTimPhieuXK.ChonPhieuXuatKho(GetPhieuXuatKho);
            f.Show(this);
        }

        private void GetPhieuXuatKho(object sender, ChonPhieuXKEventArgs e)
        {
            string mapxk = e.MaPhieuXuatKho;

            this.HienthiPHIEU_XUAT_HANG(mapxk);
            this.HienthiCHI_TIET_PXH(mapxk);
            this.HienthiCHI_TIET_GT_ND(mapxk);
        }

        private void frmHienthiPhieuXK_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }
    }
}